<template>
	<view class="supplier-home">
		<!-- 导航栏 -->
		<view class="nav">
			<view :style="{ height: `${systemInfo.statusBarHeight}px` }"></view>
			<view class="">
				<view class="nav-head">
					<u-icon style="margin: 0 20rpx;" name="arrow-left" @click="goOff()"></u-icon>
					<span>{{i18n.Title}}</span>
				</view>
			</view>
			<view class="store-head">
				<image class="store-head-img" :src="wholesale.mer_avatar" mode="widthFix"></image>
				<span>
					<view class="store-head-title">
						{{ wholesale.mer_name }}
						<u-icon style="margin: 0 10rpx;" name="arrow-right"></u-icon>
					</view>
					<view class="store-head-sub">{{i18n.Sold}}&nbsp;{{ wholesale.sales }}件</view>
				</span>
			</view>
		</view>
		<!-- 全部商品 -->
		<view class="goods-sum">
			<view class="goods-sum-fl">{{i18n.Allgoods}}&nbsp;({{ storelist.length }})</view>
			<view class="goods-sum-fr" @click="listcheck = !listcheck">
				<image src="../static/images/check1.png" v-if="listcheck"></image>
				<image src="../static/images/check2.png" v-if="!listcheck"></image>
			</view>
		</view>
		<!-- 商品列表 -->
		<scroll-view scroll-y="true" style="flex: 1; min-height: 0rpx;"  @scrolltolower="loadmore()">
			<view class="goods-list-box">
				<view :key="item.product_id" v-for="(item, index) in storelist" v-show="listcheck" @click="goGoodsDetail(item.product_id,item.supplier_id)">
					<view class="goods-list">
						<image :src="item.image"></image>
						<view class="goods-list-head">{{ item.store_name }}</view>
						<view class="goods-list-bady">
							<view class="goods-list-bady-fl">
								<view class="money">
									{{i18n.Commission}}:
									<i></i>
								</view>
								<view class="prece">{{i18n.Price}}:&nbsp;￥{{ item.price }}</view>
							</view>
							<view class="goods-list-bady-fr">
								<image class="add-img" src="../static/images/product_add.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) == -1 && !checked && !item.is_added" ></image>
								<image class="add-img" src="../static/images/product_added.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) != -1 && !checked" ></image>
								<image class="add-img" src="../static/images/product_added.png"  @click.stop="noaddgoods" v-if="item.is_added" ></image>
								<image class="add-img" src="../static/images/select_selected.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) != -1 && checked && !item.is_added" ></image>
							</view>
						</view>
					</view>
				</view>
				<view v-for="item in storelist" :key="item.product_id" class="straight" v-if="!listcheck" @click="goGoodsDetail(item.product_id,item.supplier_id)">
					<view class="image"><image :src="item.image"></image></view>
					<view class="text">
						<view class="name">{{ item.store_name }}</view>
						<view class="money-wrap">
							<view class="">
								<view class="money">
									<span>{{i18n.Commission}}:</span>
									<text></text>
								</view>
								<view class="price">{{i18n.Price}}:￥{{ item.price }}</view>
							</view>
							<view class="addimg">
								<image class="add-img" src="../static/images/product_add.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) == -1 && !checked && !item.is_added" ></image>
								<image class="add-img" src="../static/images/product_added.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) != -1 && !checked" ></image>
								<image class="add-img" src="../static/images/product_added.png"  @click.stop="noaddgoods" v-if="item.is_added" ></image>
								<image class="add-img" src="../static/images/select_selected.png"  @click.stop="addgoods(item.product_id)" v-if="addimgarr.indexOf(item.product_id) != -1 && checked && !item.is_added" ></image>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view style="text-align: center; margin-bottom: 10rpx;">{{loadTitle}}</view><!-- 加载更多 -->
		</scroll-view>
		<!-- 加入我的店家 -->
		<view class="add-store">
			<view class="">
				<label>
					<checkbox @click="delallfun" :checked="checked"/>
					<text>{{i18n.CheckAll}}</text>
				</label>
			</view>
			<view class="add-store-fr">
				<view class="goods-sum" @click="selecAttr = !selecAttr">
					{{i18n.Gong}}{{ addgoodlist.length }}{{i18n.Jian}}
					<u-icon name="arrow-up" color="#bdbdbd" size="32" style="margin-left:20rpx;"></u-icon>
				</view>
				<button class="addgoods-btn" @click="pushgoods">{{i18n.Tianjia}}</button>
			</view>
		</view>
		<!-- 购物车 -->
		<view style="position: fixed; bottom: 0; z-index: 999999;">
			<u-popup v-model="selecAttr" mode="bottom" border-radius="28" height="770rpx" width="100%">
				<view class="cart">
					<scroll-view scroll-y="true" style="height: 100%;">
						<view class="cart-item" v-for="(item, index) in addgoodlist" :key="item.product_id" @click="goGoodsDetail(item.product_id,item.supplier_id)">
							<view class="stor-list">
								<view class="list-fl"><image :src="item.image"></image></view>
								<view class="list-fr">
									<view class="list-head">{{ item.store_name }}</view>
									<view class="list-Commission">
										{{i18n.Commission}}:
										<i>{{ item.Commission }}</i>
									</view>
									<view class="list-bottom">
										<view class="price">{{i18n.Price}}:{{ item.price }}</view>
										<image class="add-img" src="../static/images/product_delete.png" @click.stop="delgoods(item.product_id)"></image>
									</view>
								</view>
							</view>
						</view>
						<image class="nogoodsimg" src="https://gbay1.dcyu.com/uploads/images/noCart.png" v-if="addgoodlist.length == 0"></image>
					</scroll-view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	const globalData = getApp().globalData;
import { supplierhome, suppliergoodslist,productadd,open_state} from '@/api/store.js';
export default {
	data() {
		return {
			listcheck: true,
			selecAttr: false,
			systemInfo: uni.getSystemInfoSync(),
			id: '', //店铺id
			background: {
				background: 'url() no-repeat',
				backgroundSize: '100%'
			},
			checked: false, //添加所有
			yesgoodslength:0,//可添加的商品长度
			wholesale: [],
			storelist: [],
			addgoodlist: [],
			addimgarr:[],
			loadTitle:globalData.$t('supplier_home').LoadMore,
			where: {
				page: 1,
				limit: 6
			},
		};
	},
	computed: {
	   i18n() {
	    return globalData.$t('supplier_home');
	   },
	  },
	methods: {
		loadmore:function(){
			setTimeout(()=>{
				this.where.limit+=6
				this.getstorelist()//获得商品信息
			},500);
		},
		goGoodsDetail:function(id,supplier_id){
			uni.navigateTo({
				url: `/pages/store/goods_detail1/index?goodsid=${id}&storeid=${this.storeid}&supplier_id=${supplier_id}`
			})
		},
		viewstatus:function(){//查看开店状态
			let that =this
			open_state().then(res=>{
				if(res.status==200){
					switch(res.data.open_state){
						case 0:
							uni.redirectTo({
							    url: '/pages/store/privatestop/index'
							});
						break;
						case 1:
							uni.redirectTo({
							    url: '/pages/store/privatestop/index'
							});
						break;
						case 3:
							uni.redirectTo({
							    url: '/pages/store/store_type/index'
							});
						break;
					}
				}
			}).catch(err=>{
				return that.$util.Tips({
					title: err
				});
			})
		},
		goOff() {//导航栏左侧图标返回上一页
			uni.navigateBack()
		},
		delallfun: function() {
			//全选商品
			let that =this
			this.checked = !this.checked;
			if (this.checked) {
				this.regain()
				this.storelist.map(item => {
					if(!item.is_added){
						that.addgoodlist.push(item)
						that.addimgarr.push(item.product_id)
						return item;
					}
				});
			} else {
				this.regain()
			}
		},
		delgoods: function(id) {
			//删除商品
			const index = this.addgoodlist.findIndex(item => item.product_id === id);
			this.addgoodlist.splice(index, 1);
			this.addimgarr.splice(index, 1);
			this.checked=false
		},
		addgoods: function(id) {
			//添加商品
			let that = this;
			let isadd = false;
			that.addgoodlist.some(item => {
				if (id == item.product_id) {
					isadd = true;
					return that.$util.Tips({
						title: that.i18n.Tianjia
					});
					return true;
				}
			});
			if (!isadd) {
				this.storelist.some(item => {
					if (id == item.product_id) {
						that.addimgarr.push(item.product_id);
						that.addgoodlist.push(item);
						isadd = false;
						return true;
					}
				});
			}
			if(that.yesgoodslength==that.addgoodlist.length){
				that.checked=true
			}
		},
		pushgoods: function() {
			//加入我的店
			let that = this;
			let idarr = { product_id: '' };
			idarr.product_id = that.addgoodlist
				.map(item => {
					return item.product_id;
				})
				.join(',');
			productadd(idarr).then(res => {
				that.getstorelist();
				if (res.status == 200) {
					that.getstorelist();
					return that.$util.Tips({
						title: res.message
					});
				}
			}).catch(res=>{
				return that.$util.Tips({
					title: res.message
				});
			})
			this.regain()
			this.checked=false
		},
		noaddgoods:function(){
			return this.$util.Tips({
				title: this.i18n.Cu
			});
		},
		//获得头部信息
		getstorehead: function() {
			let that = this;
			supplierhome({id:this.id})
				.then(res => {
					console.log(res)
					that.wholesale = res.data;
				})
				.catch(err => {
					uni.showToast({
						title: err
					});
				});
		},
		//商品列表
		getstorelist: function() {
			let that = this;
			this.where ={id:this.id,page: this.where.page,
				limit: this.where.limit}
			suppliergoodslist(this.where)
				.then(res => {
					that.storelist = res.data.list;
					let loadend =res.data.list.length < that.where.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? that.i18n.YiLoadMore : that.i18n.LoadMore;
					that.yesgoodslength=0
					res.data.list.map(res=>{
						if(!res.is_added){
							that.yesgoodslength++
						}
					})
				})
				.catch(err => {
					uni.showToast({
						title: err
					});
				});
		},
		regain:function(){
			this.addgoodlist = undefined;
			this.addgoodlist = new Array();
			this.addimgarr = undefined;
			this.addimgarr = new Array();
		}
	},
	onReady() {
		
	},
	onLoad(option) {
		console.log(option)
		this.id = option.supplierid;
	},
	onShow() {
		this.getstorelist();
		this.getstorehead();
		this.viewstatus();
	}
};
</script>

<style lang="scss">
.supplier-home{
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	display: flex;
	flex-direction: column;
	padding-bottom: 100rpx;
	overflow: hidden;
}
.nav {
	/* #ifdef MP */
	height: 272rpx;
	/* #endif */
	/* #ifdef H5 */
	height: 215rpx;
	/* #endif */
	background-color: pink;
	.nav-head {
		display: flex;
		flex-wrap: nowrap;
		/* #ifdef MP */
		margin-top: 12rpx;
		/* #endif */
		/* #ifdef H5 */
		padding-top: 10rpx;
		/* #endif */
		height: 50rpx;
		font-size: 36rpx;
		font-weight: bold;
		color: #ffffff;
		span {
			display: inline-block;
			flex: 1;
			text-align: center;
			padding-right: 10%;
		}
	}
}
.store-head {
	display: flex;
	flex-wrap: wrap;
	margin: 40rpx 0 0 20rpx;
	left: 25rpx;
	top: 105rpx;
	height: 80rpx;
	width: 100%;
	.store-head-img {
		width: 76rpx;
		height: 76rpx;
		background: #ffffff;
		border-radius: 8rpx;
	}
	span {
		margin: 0 20rpx;
	}
	.store-head-title {
		margin-bottom: 5rpx;
		font-weight: bold;
		font-size: 32rpx;
		color: #ffffff;
		image {
			width: 36rpx;
			height: 36rpx;
		}
	}
	.store-head-sub {
		font-size: 22rpx;
		color: #ffffff;
	}
}
.goods-sum {
	background-color: #ffffff;
	display: flex;
	justify-content: center;
	justify-content: space-between;
	padding: 30rpx 20rpx;
	font-size: 400;
	.goods-sum-fl {
		font-size: 32rpx;
		font-weight: bold;
	}
	.goods-sum-fr {
		image {
			width: 40rpx;
			height: 40rpx;
		}
	}
}

.goods-list-box {
	display: flex;
	flex-wrap: wrap;
	white-space: normal;
	justify-content: space-between;
	padding: 20rpx 20rpx 0 20rpx;
	.straight {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		height: 238rpx;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		background-color: #ffffff;
		padding: 24rpx 24rpx;
		.image {
			width: 190rpx;
			height: 190rpx;
			image {
				display: block;
				width: 100%;
				height: 100%;
			}
		}

		.text {
			padding: 0 20rpx 0;
			flex: 1;
			.name {
				text-overflow: -o-ellipsis-lastline;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				height: 80rpx;
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
			}

			.money-wrap {
				display: flex;
				align-items: center;
				flex-wrap: nowrap;
				justify-content: space-between;
				.money {
					display: flex;
					flex-wrap: nowrap;
					width: 100%;
					margin-top: 12rpx;
					font-size: 26rpx;
					color: #fd342a;
					font-size: 24rpx;
					span {
						display: block;
						height: 48rpx;
						margin-right: 10rpx;
					}
					text {
						font-weight: bold;
						font-size: 34rpx !important;
						line-height: 1;
					}
				}
				.price {
					width: 100%;
					height: 33rpx;
					font-size: 24rpx;
					font-weight: bold;
					color: #222222;
				}
				.addimg {
					width: 48rpx !important;
					height: 48rpx !important;
					background: #eaeaea;
					border-radius: 50%;
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
}
.goods-list {
	margin-bottom: 20rpx;
	padding: 12rpx;
	width: 345rpx;
	height: 554rpx;
	background: #ffffff;
	border-radius: 20rpx;
	image {
		width: 100%;
		height: 345rpx;
		background: #ffffff;
	}
	.goods-list-head {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		width: 100%;
		height: 80rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
	}
}
.goods-list-bady {
	margin-top: 12rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	.goods-list-bady-fl {
		.money {
			margin-bottom: 8rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: flex-end;
			color: #fd342a;
			font-size: 24rpx;
		}
		i {
			margin-left: 10rpx;
			font-size: 34rpx !important;
			font-weight: bold;
		}
		.prece {
			font-size: 24rpx;
			font-weight: 400;
		}
	}
	.goods-list-bady-fr {
		image {
			margin-top: 10rpx;
			width: 48rpx;
			height: 48rpx;
			border-radius: 50%;
		}
	}
}
//加入我的店
.add-store {
	z-index: 999999999999;
	position: fixed;
	bottom: 0;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0 30rpx;
	width: 100%;
	height: 98rpx;
	background-color: #ffffff;
	.add-store-fr {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		.goods-sum {
			image {
				padding: 0 40rpx 0 16rpx;
				width: 24rpx;
				height: 24rpx;
			}
		}
		.addgoods-btn {
			width: 260rpx;
			height: 80rpx;
			background: #fd342a;
			border-radius: 40px;
			font-size: 32rpx;
			font-weight: 400;
			line-height: 80rpx;
			color: #ffffff;
		}
	}
}
.cart {
	z-index: 9999;
	height: 680rpx;
	background: #ffffff;
	border-radius: 20px 20px 0px 0px;
	padding: 40rpx 30rpx 0 30rpx;
	.cart-item {
		overflow: hidden;
		height: 202rpx;
		background-color: #ffffff;
		.stor-list {
			display: flex;
			flex-wrap: wrap;
			.list-fl {
				margin-right: 20rpx;
				width: 141rpx;
				height: 141rpx;
				background: #ffffff;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.list-fr {
				flex: 1;
				padding-bottom: 30rpx;
				border-bottom: 1px solid #eaeaea;
				.list-head {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					width: 432rpx;
					font-size: 28rpx;
					font-weight: bold;
					margin-bottom: 12rpx;
					color: #222222;
				}
				.list-sub {
					margin-right: 12rpx;
					width: 112rpx;
					height: 34rpx;
					background: #ffece8;
					border-radius: 17px;
					font-size: 20rpx;
					font-weight: 400;
					line-height: 34rpx;
					text-align: center;
					color: #de5855;
				}
				.list-Commission {
					display: flex;
					align-items: flex-end;
					font-weight: 400;
					color: #fd342a;
					font-size: 24rpx;
					i {
						margin-left: 10rpx;
						font-size: 34rpx;
						font-weight: bold;
						color: #fd342a;
					}
				}
				.list-bottom {
					display: flex;
					justify-content: space-between;
					margin-top: 8rpx;
					height: 48rpx;
					.prece {
						font-size: 24rpx;
						font-weight: 400;
						line-height: 33px;
						color: #222222;
					}
					.add-img {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}
	}
	.nogoodsimg {
		width: 400rpx;
		height: 300rpx;
		margin: 150rpx 150rpx;
	}
}
</style>
